#!/bin/bash

cd /root/kube_master_src

# 环境变量加入seesion
source ./config/config.ini

# 生成 kubeadm-init.yaml
cd /root/kube_master_src/config
cat > kubeadm-init.yaml <<EOF
---
apiVersion: kubeadm.k8s.io/v1beta1
kind: InitConfiguration
# change imageRepository
nodeRegistration:
  kubeletExtraArgs:
    pod-infra-container-image: {{DOCKER_SERVER}}/google_containers/pause-amd64:3.2
---
apiVersion: kubeadm.k8s.io/v1beta1
kind: ClusterConfiguration
kubernetesVersion: v$KUBE_VERSION
controlPlaneEndpoint: $MASTER_VIP:8443
apiServer:
  certSANs:
  - "$MASTER_HOSTNAME_1"
  - "$MASTER_HOSTNAME_2"
  - "$MASTER_HOSTNAME_3"
  - $MASTER_IP_1
  - $MASTER_IP_2
  - $MASTER_IP_3
  - $MASTER_VIP
  - kubernetes
  - kubernetes.default
  - kubernetes.default.svc
  - kubernetes.default.svc.cluster
  - kubernetes.default.svc.cluster.local
  - 10.96.0.1
  - 127.0.0.1
etcd:
  external:
    endpoints:
    - "https://$ETCD_IP_1:2379"
    - "https://$ETCD_IP_2:2379"
    - "https://$ETCD_IP_3:2379"
    caFile: /etc/kubernetes/pki/etcd/etcd-ca.pem
    certFile: /etc/kubernetes/pki/etcd/etcd.pem
    keyFile: /etc/kubernetes/pki/etcd/etcd-key.pem
networking:
  podSubnet: 10.244.0.0/16
  dnsDomain: cluster.local
  serviceSubnet: 10.96.0.0/12
imageRepository: "{{DOCKER_SERVER}}/google_containers"
clusterName: $CLUSTER_NAME
---
apiVersion: kubeproxy.config.k8s.io/v1alpha1
kind: KubeProxyConfiguration
mode: ipvs
EOF

# 创建集群
kubeadm init --config kubeadm-init.yaml --upload-certs > kubeadm-init.log

# 保存 join command
sleep 60
kubeadm token create --print-join-command > kubeadm-join-worker.sh
certificatekey=$(cat kubeadm-init.log|grep "\-\-certificate\-key"|awk '{print $(NF-1),$(NF)}')
cat > kubeadm-join-master.sh <<EOF
`cat kubeadm-join-worker.sh` --control-plane $certificatekey
EOF
